/*******************************************************************************
*Project :          COVID-19 Research Project 

*Purpose : 		    Create maps for stay at home orders 

*Source : 			Womply Data from AWS 


*******************************************************************************/

clear all 
set more off 
capture log close 


*===============================================================================
* Stay at Home Maps 
*===============================================================================

* load data 
u "data/clean/stay_at_home/stay_at_home_AM.dta" , clear

* save old version for R 
saveold "data/clean/stay_at_home/stay_at_home_forR.dta" , version(12) replace 

* sort data 
sort county_fips date 

* gen date count
  egen day_id = group(date) 
  
* get day groups 
  xtile day_id_groups = day_id , nq(10)
  
  forv gr = 1/10 { 
	di as error "group =`gr'" 
	tab date if day_id_groups == `gr'
	} 
	
  forv day = 1/22 { 
	di as error "day_id = `day'" 
	tab date if day_id == `day'
	} 
  
  
* keep one observation per fips 
  gen flag = 1 if effective_date == date
  keep if flag == 1
  

* save data 
saveold "data/clean/stay_at_home/stay_at_home_maps.dta" , version(12) replace



*===============================================================================
* Stay at Home Maps - State Groups
*===============================================================================

* load data 
u "data/clean/stay_at_home/stay_at_home_AM.dta" , clear

* put states in to groups 
summ date 
keep if date == `r(max)' 
tab date 
duplicates report county_fips 

* states with no stay_at_home orders 
bys state_fips : egen max_stay = max(stay_at_home) 
tab max_stay 
g no_sah  = 1 if max_stay == 0 
tab state if no_sah  == 1 

* states with only state-wide order 
egen unique_dates = nvals(effective_date) , by(state_fips)
tab unique_dates 
g only_st_sah = 1 if unique_dates == 1 
tab state if only_st_sah == 1 

* states with some counties with an order 
bys state_fips : gen no_counties = _N 
bys state_fips : egen sum_sah = total(stay_at_home) 
g frac_sah = sum_sah/no_counties
tab frac_sah 
g some_ct_sah = 1 if frac_sah!=0 & frac_sah!=1 
tab state if some_ct_sah == 1


* states in which some counties announced an order before state-wide 
g heter_sah = 1 if frac_sah == 1 & unique_dates>1 
tab state if heter_sah == 1

bys state_fips : keep if _n ==1 
keep state_fips state  no_sah only_st_sah some_ct_sah heter_sah

* sanity test 
foreach var in  no_sah only_st_sah some_ct_sah heter_sah { 
	replace `var' = 0 if missing(`var')
	} 

gen test = no_sah + only_st_sah + some_ct_sah + heter_sah 
tab test

* gen group variable
  g st_group = 1 if        only_st_sah == 1 
  replace st_group = 2 if  heter_sah == 1 
  replace st_group = 3 if  some_ct_sah == 1 
  replace st_group = 4 if  no_sah == 1 
  tab st_group 
  
* get group labels 
  g st_group_lbl = "No  order" if no_sah == 1 
  replace st_group_lbl = "State-wide orders only" if only_st_sah == 1 
  replace st_group_lbl = "Local orders only"  if some_ct_sah == 1  
  replace st_group_lbl = "State-wide orders, preceded by local orders" if heter == 1 
  
* correct state_fips for R 
  tostring state_fips , replace
  replace state_fips = "0" + state_fips if length(state_fips)==1 
  tab state_fips 
  ren state_fips fips 
  
 * save 
   keep fips st_* 
   compress 
   saveold "data/clean/stay_at_home/stay_at_home_stmaps.dta" , version(12) replace


*===============================================================================
* Womply per - capita maps 
*===============================================================================

* load womply daily revenue data 
  use "data/clean/womply/womply_daily_revenue.dta" , clear 

  
* get overall category variable 
  gen clean_cat="Other" if category!="overall"
  replace clean_cat= "Restaurant" if inlist(category,"bars and lounges","food and beverage shops","quick serve food and beverage businesses","restaurants")
  tab category clean_cat, m
  
  
* subset data to March 2020 
  keep if inrange(date, td(01mar2020) , td(07mar2020))

* collapse revenue to categories 
  collapse (sum) revenue , by(county_fips clean_cat) 
  
* get population values - 99% of data merges 
  merge m:1 county_fips using "data/clean/usda_ers/county_information.dta" , keep(3) nogen ///
	keepusing(pop_estimate_2018) 

* reshape data 
  rename revenue rev_
  reshape wide rev_  , i(county_fips) j(clean_cat) string
  
* replace missing values , generate per capita
  foreach var of varlist rev_* { 
	replace `var' = 0 if missing(`var')
	gen pc_`var' = `var'/pop_estimate_2018 
	}
	
  
* collapse to county fips 
  ren county_fips fips 
  
* change fips to character 
  tostring fips , replace
  replace fips = "0" + fips if length(fips) == 4 
  
* convert revenue to groups 
  xtile gr_Other = pc_rev_Other , nq(10) 
  xtile gr_Restaurant = pc_rev_Restaurant , nq(10) 

  saveold "data/clean/womply/womply_formaps.dta" , version(12) replace
